import Vue from 'vue'
import 'babel-polyfill' // 让ie不空白
import 'normalize.css/normalize.css' // A modern alternative to CSS resets

import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import locale from 'element-ui/lib/locale/lang/zh-CN' // lang i18n

import '@/styles/index.scss' // global css
import echarts from 'echarts'
import 'vue2-animate/dist/vue2-animate.min.css'

import App from './App'
import store from './store'
import router from './router'
import axios from 'axios'
import { getToken } from '@/utils/auth'

import qs from 'qs'

import '@/icons' // icon
import '@/permission' // permission control

/**
 * This project originally used easy-mock to simulate data,
 * but its official service is very unstable,
 * and you can build your own service if you need it.
 * So here I use Mock.js for local emulation,
 * it will intercept your request, so you won't see the request in the network.
 * If you remove `../mock` it will automatically request easy-mock data.
 */

Vue.use(ElementUI, { locale })
Vue.prototype.$echarts = echarts

// 添加请求拦截器
axios.interceptors.request.use(function(config) {
  // 在发送请求之前做某事
  config.baseURL = 'http://192.168.0.146:8090'
  // config.baseURL = 'https://wechatapp.gzsjht.com'
  if (store.getters.token) {
    config.headers['X-Token'] = getToken() // 让每个请求携带自定义token 请根据实际情况自行修改
  }
  if ((config.method === 'post' || config.method === 'put') && config.url !== '/CustomMenu/add' && config.url !== '/Material/addMaterial') {
    const data = JSON.parse(JSON.stringify(config.data))
    for (const index in data) {
      if (data[index] instanceof Array) {
        data[index] = data[index].join(',')
      }
    }
    config.data = qs.stringify(data)
  }
  return config
}, function(error) {
  // 请求错误时做些事
  return Promise.reject(error)
})

Vue.prototype.$http = axios

Vue.config.productionTip = false

new Vue({
  el: '#app',
  router,
  store,
  render: h => h(App)
})
